import 'package:casdon/core/network/api_client_provider.dart';
import 'package:casdon/modules/login/data/repository/auth_repository.dart';
import 'package:casdon/modules/login/data/repository/auth_repository_iml.dart';
import 'package:casdon/modules/login/domain/usecases/login_usecase.dart';
import 'package:casdon/modules/login/domain/validators/email_validator.dart';
import 'package:casdon/modules/login/domain/validators/input_validator.dart';
import 'package:casdon/modules/login/domain/validators/password_validator.dart';
import 'package:casdon/modules/login/domain/validators/phone_validator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// 仓库
final authRepositoryProvider = Provider<AuthRepository>(
  (ref) => AuthRepositoryImpl(ref.read(apiClientProvider)),
);

// 帐户校验
final phoneValidatorProvider = Provider<PhoneValidator>(
  (ref) => PhoneValidator(),
);
final emailValidatorProvider = Provider<EmailValidator>(
  (ref) => EmailValidator(),
);

// 密码格式校验
final passwordValidatorProvider = Provider<InputValidator>(
  (ref) => PasswordValidator(),
);

// 登录
final loginUsecaseProvider = Provider(
  (ref) => LoginUsecase(
    ref.read(authRepositoryProvider),
    ref.read(inputValidatorProvider),
  ),
);

// email 和 phone 校验，只要一个通过就是true
final inputValidatorProvider = Provider<InputValidator>(
  (ref) => CombinedValidator([
    ref.read(phoneValidatorProvider),
    ref.read(emailValidatorProvider),
  ], type: CombinedType.or),
);

// 多页面独立控制
final editingControllerProvider = Provider.autoDispose
    .family<TextEditingController, String>((ref, id) {
      final controller = TextEditingController();
      return controller;
    });
